/*
  Copyright (c) 2020 Sogou, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#ifndef __RPC_CONTEXT_H__
#define __RPC_CONTEXT_H__

#ifdef _WIN32
#include <workflow/PlatformSocket.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif
#include <string>
#include <functional>
#include <workflow/Workflow.h>


namespace frpc
{


class RPCContext
{
public:
	virtual long long get_seqid() const = 0;
	virtual std::string get_remote_ip() const = 0;
	virtual int get_peer_addr(struct sockaddr *addr, socklen_t *addrlen) const = 0;
	virtual const std::string& get_service_name() const = 0;
	virtual const std::string& get_method_name() const = 0;

	virtual SeriesWork *get_series() const = 0;

public:
	// for client-done
	virtual bool success() const = 0;
	virtual int get_status_code() const = 0;
	virtual const char *get_errmsg() const = 0;
	virtual int get_error() const = 0;
	virtual void *get_user_data() const = 0;
	//virtual int get_timeout_reason() const;

public:
	// for server-process
	//virtual void set_data_type(RPCDataType type) = 0;//enum RPCDataType
	virtual void set_compress_type(CompressType type) = 0;//enum RPCCompressType
	//virtual void set_attachment_nocopy(const char *attachment, size_t len) = 0;
	//virtual bool get_attachment(const char **attachment, size_t *len) const = 0;

	virtual void set_reply_callback(std::function<void (RPCContext *ctx)> cb) = 0;
	virtual void set_send_timeout(int timeout) = 0;
	virtual void set_keep_alive(int timeout) = 0;
	//virtual void log(const RPCLogVector& fields) = 0;
//	virtual void baggage(const std::string& key, const std::string& value) = 0;
	//virtual void noreply();
	//virtual WFConnection *get_connection();

public:
	virtual ~RPCContext() { }
};




template<class RPCREQ, class RPCRESP>
class RPCContextImpl : public RPCContext
{
public:
	long long get_seqid() const override
	{
		return task_->get_task_seq();
	}

	std::string get_remote_ip() const override
	{
		char ip_str[INET6_ADDRSTRLEN + 1] = { 0 };
		struct sockaddr_storage addr;
		socklen_t addrlen = sizeof(addr);

		if (this->get_peer_addr((struct sockaddr*)&addr, &addrlen) == 0)
		{
			if (addr.ss_family == AF_INET)
			{
				struct sockaddr_in* sin = (struct sockaddr_in*)(&addr);

				inet_ntop(AF_INET, &sin->sin_addr, ip_str, addrlen);
			}
			else if (addr.ss_family == AF_INET6)
			{
				struct sockaddr_in6* sin6 = (struct sockaddr_in6*)(&addr);

				inet_ntop(AF_INET6, &sin6->sin6_addr, ip_str, addrlen);
			}
		}

		return std::string(ip_str);
	}

	int get_peer_addr(struct sockaddr* addr, socklen_t* addrlen) const override
	{
		return task_->get_peer_addr(addr, addrlen);
	}

	const std::string& get_service_name() const override
	{
		return task_->get_req()->get_service_name();
	}

	const std::string& get_method_name() const override
	{
		return task_->get_req()->get_method_name();
	}



	void set_compress_type(CompressType type) override
	{
		task_->get_resp()->set_compress_type(int(type));
	}

	void set_send_timeout(int timeout) override
	{
		task_->set_send_timeout(timeout);
	}

	void set_keep_alive(int timeout) override
	{
		task_->set_keep_alive(timeout);
	}

	SeriesWork* get_series() const override
	{
		return series_of(task_);
	}

	void* get_user_data() const override
	{
		return task_->user_data;
	}



public:
	// for client-done
	bool success() const override
	{
		return task_->get_resp()->get_status_code() == 0;
	}

	int get_status_code() const override
	{
		return task_->get_resp()->get_status_code();
	}

	const char* get_errmsg() const override
	{
		return task_->get_resp()->get_errmsg();
	}

	int get_error() const override
	{
		return task_->get_resp()->get_error();
	}
	//int get_timeout_reason() const override;

public:



	void set_reply_callback(std::function<void(RPCContext* ctx)> cb) override
	{
		if (this->is_server_task())
		{
			if (cb)
			{
				task_->set_callback([this, cb](SubTask* task) {
					cb(this);
					});
			}
			else
				task_->set_callback(nullptr);
		}
	}


	RPCREQ* get_request() {
		return task_->get_req();
	}
	RPCRESP* get_response() {
		return task_->get_resp();
	}
	void set_task(WFNetworkTask<RPCREQ, RPCRESP>* task) { task_=task; };
	RPCContextImpl(WFNetworkTask<RPCREQ, RPCRESP>* task=nullptr) : task_(task) { }

protected:
	bool is_server_task() const
	{
		return task_->get_state() == WFT_STATE_TOREPLY
			|| task_->get_state() == WFT_STATE_NOREPLY;
	}

protected:
	WFNetworkTask<RPCREQ, RPCRESP>* task_;
};

using Context = RPCContextImpl<Request, Reply>;
} 

#endif

